package juc.concurrent.phaser;

import java.util.concurrent.Phaser;

public class PhaserDemo {
    public static void main(String[] args) {
        Phaser phaser = new Phaser(3);
        for (int i = 0; i < 3; i++) {
            ThreadA threadA = new ThreadA(phaser);
            threadA.setName(i+"");
            threadA.start();
        }
    }

    static class ThreadA extends Thread {
        private Phaser phaser;

        public ThreadA(Phaser phaser) {
            this.phaser = phaser;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " A begin " + System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName() + " 第几屏障 " + phaser.getPhase());
            phaser.arriveAndAwaitAdvance();
            System.out.println(Thread.currentThread().getName() + " A end " + System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName() + " 第几屏障 " + phaser.getPhase());
            System.out.println(Thread.currentThread().getName() + " 总数量 " + phaser.getArrivedParties());
            phaser.arriveAndAwaitAdvance();
            System.out.println(Thread.currentThread().getName() + " 第几屏障 " + phaser.getPhase());
            System.out.println(Thread.currentThread().getName() + " A eeee " + System.currentTimeMillis());
        }
    }
}
